package com.nine.controller;

import com.github.pagehelper.Page;
import com.nine.entity.MyReturn;
import com.nine.entity.PageSort;
import com.nine.entity.Role;
import com.nine.entity.Userinfo;
import com.nine.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author:LouFangRui
 * @Date:2022/6/28 15:49
 */
@RestController
@RequestMapping("role")
public class RoleController extends ControllerFather {
	@Autowired
	private RoleServiceImpl roleService;
	MyReturn myReturn=new MyReturn();

	/**
	 * 删除和批量删除
	 * */
	@RequestMapping("delete.do")
	public MyReturn deleteRole(String[] roleId){
		int delete = roleService.deleteById(roleId);
		System.out.println("删除的id是"+roleId.toString());
		if (delete>0){
			return super.getSuccessResult(myReturn);
		}else {
			return super.getFailResult(myReturn);
		}
	}
	/***
	 *添加
	 */
	@RequestMapping("addRole.do")
	public MyReturn addRole(Role role,HttpSession session){
		//判断前端开关状态并赋值
		if(role.getStatus()!=null){
			role.setStatus("0");
		}else{
			role.setStatus("1");
		}

		//从session中获取登录者的名字，放入实体中作为添加人的值
		Userinfo userInfo = (Userinfo) session.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		role.setCreateBy(userName);
		//获取登录者信息，并保存到实体中


		// 当前添加时间
		role.setCreateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));
		int b = roleService.addRole(role);

		if (b>0){
			return super.getSuccessResult(myReturn);
		}else {
			return super.getFailResult(myReturn);
		}
	}
	/**
	 * 分页
	 * */
	@RequestMapping( "findAllRole.do")
	public MyReturn findAllUser(PageSort pageSort, Role role) {
		Page page = roleService.findAllRole(pageSort,role);
		MyReturn myReturn = new MyReturn();
		//获取分页之后的数据
		myReturn.setData(page.getResult());
		//获取总条数
		myReturn.setCount(page.getTotal());
		return super.getSuccessResult(myReturn);
	}
	/**
	 * 修改
	 * */
	@RequestMapping("updateRole.do")
	public MyReturn updateRole(Role role, HttpSession session) {
		//判断前端开关状态并赋值
		if(role.getStatus()!=null){
			role.setStatus("0");
		}else{
			role.setStatus("1");
		}

		//从session中获取登录者的名字，放入实体中作为修改人的值
		Userinfo userInfo = (Userinfo) session.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		role.setUpdateBy(userName);

		// 当前添加时间
		role.setUpdateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));
		int b = roleService.updateRole(role);
		if (b>0){
			System.out.println("修改成功");
			return super.getSuccessResult(myReturn);
		}else {
			return super.getFailResult(myReturn);
		}
	}
}
